package com.example.product.controller;


import com.example.common.result.UserResult;
import com.example.model.product.dto.GoodsDetailVO;
import com.example.model.product.pojo.BnnerImage;
import com.example.model.product.pojo.Goods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.product.service.GoodsService;
import com.example.model.product.dto.CategoriesGoodVO;

import java.util.List;

@RestController
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 一级分类
     * @return
     */
    @GetMapping("/user/home/category/head")
    public UserResult selectAllGoods(){
        List<CategoriesGoodVO> page = goodsService.selectAllGoods();
        return UserResult.success(page);
    }

    /**
     * 二级分类商品
     * @param id
     * @return
     */
    @GetMapping("/user/category")
    public UserResult selectGoods(String id){
        String key = "good_category"+id;
        //先查看redis中有没有
        List<Goods> list = (List<Goods>) redisTemplate.opsForValue().get(key);
        //如果没有就从数据库中查找然后放入redis中
        if (list != null && list.size() > 0) {
            return UserResult.success(list);
        }
        list = goodsService.selectDiffrentCategoriesGoods(id);
        log.info("{}保存goods",list);
        redisTemplate.opsForValue().set(key,list);
        return UserResult.success(list);
    }

    /**
     * 轮播图
     * @return
     */
    @GetMapping("/user/home/banner")
    public UserResult selectGoods(){
        List<BnnerImage> list = goodsService.selectBnnerimage();
        return UserResult.success(list);
    }

    /**
     * 新鲜好物
     * @return
     */
    @GetMapping("/user/home/news")
    public UserResult selectNewsGoods(){
        List<Goods> list = goodsService.selectNewsGoods();
        return UserResult.success(list);
    }
    @GetMapping("/user/home/hot")
    public UserResult selectHotGoods(){
        List<Goods> list = goodsService.selectHotGoods();
        return UserResult.success(list);
    }
    /**
     * 商品细节
     * @param id
     * @return
     */
    @GetMapping("/user/goods{id}")
    public UserResult selectGoodDetail(String id){
        GoodsDetailVO goods = goodsService.selectGoodDetail(id);
        return UserResult.success(goods);
    }
}
